home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
C
/
System
/
ScreenDaemon 1.2
/
ScreenDaemon appe
/
ScreenDaemon.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-05-07
|
11KB
|
381 lines
/*********************************************************************
* ScreenDaemon
* Copyright ©1994-1995 by Mason L. Bliss
* All Rights Reserved
*
* A simple gamma-table-fading screen saver.
*
* Version 1.1 2/19/1995 by Mason L. Bliss
*
*********************************************************************/
#include <AppleEvents.h>
#include <GestaltEqu.h>
#include <LowMem.h>
#include <Palettes.h>
#include "SD.h"
#include "ScreenDaemon.h"
#include "gamma.h"
#define kDelayTicks 45 /* = three-quarters of a second */
#define kFadeRate 5 /* must divide evenly into 100 */
Boolean gRunning = true,
gBadStart = false;
long gLastNap;
AEEventHandlerUPP DoAEQuitAppUPP,
DoAEOpenDocUPP,
DoAEPrintDocUPP,
DoAEOpenAppUPP;
PatchGlobalsPtr pgPtr = NULL;
/*********************************************************************
* main:
*
* Set up our daemon and then drop into the main event loop.
*
*********************************************************************/
void main(void)
{
RgnHandle cursorRgn = NULL;
EventRecord theEvent;
/* Initialize appropriate managers and install AppleEvent handlers and stuff */
SetUp();
/* Main event loop */
while (gRunning) {
WaitNextEvent(everyEvent, &theEvent, 30, cursorRgn); /* Get an event... */
HandleEvent(&theEvent); /* ...and process it. */
}
/* Remove our AppleEvent handlers and the gamma tools and such */
CleanUp();
}
/*********************************************************************
* SetUp:
*
* Set up the appropriate managers and stuff.
*
*********************************************************************/
void SetUp(void)
{
long result;
GDHandle theDevice;
Rect screenRect;
#if GENERATINGPOWERPC
ReleaseResource(GetResource('CODE',1));
#endif
MaxApplZone(); /* Take over the whole partition */
InitGraf(&qd.thePort);
DoAEQuitAppUPP = NewAEEventHandlerProc(DoAEQuitApp);
DoAEOpenDocUPP = NewAEEventHandlerProc(DoAEOpenDoc);
DoAEPrintDocUPP = NewAEEventHandlerProc(DoAEPrintDoc);
DoAEOpenAppUPP = NewAEEventHandlerProc(DoAEOpenApp);
if (AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, DoAEOpenAppUPP, 0, false) != noErr ||
AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, DoAEQuitAppUPP, 0, false) != noErr ||
AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, DoAEOpenDocUPP, 0, false) != noErr ||
AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, DoAEPrintDocUPP, 0, false) != noErr) {
SysBeep(5);
gRunning = false;
gBadStart = true;
}
/* Try to get the address of our patch globals */
if (Gestalt('sDmn', &result) == noErr) {
pgPtr = (PatchGlobalsPtr) result;
SetupGammaTools();
} else {
gRunning = false;
}
/* Get our screen coordinates */
theDevice = LMGetMainDevice();
screenRect = (*(*theDevice)->gdPMap)->bounds;
// BlockMoveData((Ptr) &((**((PixMapHandle) (**theDevice).gdPMap)).bounds),
// &screenRect, sizeof(Rect));
pgPtr->pgCorners[0].left = screenRect.left - 1; // top left
pgPtr->pgCorners[0].right = screenRect.left + 8;
pgPtr->pgCorners[0].top = screenRect.top - 1;
pgPtr->pgCorners[0].bottom = screenRect.top + 8;
pgPtr->pgCorners[1].left = screenRect.right - 8; // top right
pgPtr->pgCorners[1].right = screenRect.right + 1;
pgPtr->pgCorners[1].top = screenRect.top - 1;
pgPtr->pgCorners[1].bottom = screenRect.top + 8;
pgPtr->pgCorners[2].left = screenRect.right - 8; // bottom right
pgPtr->pgCorners[2].right = screenRect.right + 1;
pgPtr->pgCorners[2].top = screenRect.bottom - 8;
pgPtr->pgCorners[2].bottom = screenRect.bottom + 1;
pgPtr->pgCorners[3].left = screenRect.left - 1; // bottom left
pgPtr->pgCorners[3].right = screenRect.left + 8;
pgPtr->pgCorners[3].top = screenRect.bottom - 8;
pgPtr->pgCorners[3].bottom = screenRect.bottom + 1;
}
/*********************************************************************
* HandleEvent:
*
* Take care of the event that's been passed to us.
*
*********************************************************************/
void HandleEvent(EventRecord *theEvent)
{
/* What did we get? */
switch (theEvent->what) {
/* Take care of any incoming AppleEvents. */
case kHighLevelEvent:
AEProcessAppleEvent(theEvent);
break;
/* We do all our work during our idle time. */
case nullEvent:
DoMaintenance();
break;
default:
break;
}
}
/*********************************************************************
* DoMaintenance:
*
* Do our periodic maintenance.
*
*********************************************************************/
void DoMaintenance(void)
{
Point mousePt;
/* Check to see if the mouse has moved */
GetMouse(&mousePt);
LocalToGlobal(&mousePt);
if (!EqualPt(mousePt, pgPtr->pgLastMouse)) { /* id est, mouse moved */
pgPtr->pgLastMouse = mousePt; /* Update last position */
pgPtr->pgLastAction = LMGetTicks(); /* Record the time */
pgPtr->pgInSleepRect = false; /* We're not narcoleptic */
if (pgPtr->pgSaverOn)
pgPtr->pgMustWake = true; /* Wake up if we need to */
}
if (!pgPtr->pgSaverOn) { /* wide-awake maintenance */
/* Does the nice user want us to fall asleep now? */
if (!pgPtr->pgInSleepRect &&
PtInRect(mousePt, &pgPtr->pgCorners[pgPtr->pgSleepRect])) {
pgPtr->pgInSleepRect = true;
/* Make it look like we haven't moved for a while. */
pgPtr->pgLastAction -= ((pgPtr->pgIdleTicks) - kDelayTicks);
} else {
/* If we're in the wake rect, we never want to sleep */
if (PtInRect(mousePt, &pgPtr->pgCorners[pgPtr->pgWakeRect]))
pgPtr->pgLastAction = LMGetTicks();
}
/* Has enough idle time passed for us to fall asleep naturally? */
if ((LMGetTicks() - pgPtr->pgLastAction) > pgPtr->pgIdleTicks)
pgPtr->pgMustSleep = true;
/* Do we need to fall asleep? */
if (pgPtr->pgMustSleep && pgPtr->pgMustSave)
FallAsleep();
} else { /* We're asleep - do sleepy-time maintenance. */
if (pgPtr->pgMustWake)
WakeUp();
else if (LMGetTicks() - gLastNap > 3600) { /* once per minute */
gLastNap = LMGetTicks();
DoGammaFade(0); /* black out the screen again, "just in case" */
}
}
}
/*********************************************************************
* CleanUp:
*
* Remove our Apple Event Handlers and stuff.
*
*********************************************************************/
void CleanUp(void)
{
/* Unload the gamma tools, if we've already loaded them. */
if (pgPtr != NULL)
DisposeGammaTools();
/* Remove the core AppleEvent handlers */
if ((AEEventHandlerUPP) DoAEOpenApp)
AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
(AEEventHandlerUPP) DoAEOpenApp, false);
if ((AEEventHandlerUPP) DoAEQuitApp)
AERemoveEventHandler(kCoreEventClass, kAEQuitApplication,
(AEEventHandlerUPP) DoAEQuitApp, false);
if ((AEEventHandlerUPP) DoAEOpenDoc)
AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
(AEEventHandlerUPP) DoAEOpenDoc, false);
if ((AEEventHandlerUPP) DoAEPrintDoc)
AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments,
(AEEventHandlerUPP) DoAEPrintDoc, false);
/* Dispose of our Universal Procedure Pointers */
if (DoAEQuitAppUPP)
DisposeRoutineDescriptor(DoAEQuitAppUPP);
if (DoAEOpenDocUPP)
DisposeRoutineDescriptor(DoAEOpenDocUPP);
if (DoAEPrintDocUPP)
DisposeRoutineDescriptor(DoAEPrintDocUPP);
}
/*********************************************************************
* FallAsleep:
*
* Fade the screen.
*
*********************************************************************/
void FallAsleep(void)
{
long level = 100;
/* Set our flags appropriately */
gLastNap = LMGetTicks(); /* Set "now" as the last time we've faded. */
pgPtr->pgSaverOn = true; /* We're now asleep. */
pgPtr->pgMustSleep = false; /* We don't have to fall asleep again. */
pgPtr->pgInSleepRect = false; /* The mouse doesn't care if it's in the sleep */
/* corner any more. */
/* Dim the screen */
while ((level -= kFadeRate) >= 0)
DoGammaFade(level);
}
/*********************************************************************
* WakeUp:
*
* Fade the screen.
*
*********************************************************************/
void WakeUp(void)
{
long level = 0;
OSErr theError;
GDHandle theDevice;
short theDepth, theFlags;
PixMapHandle thePixMap;
/* Unfade */
while ((level += kFadeRate) <= 100)
DoGammaFade(level);
/* Reset some flags. */
pgPtr->pgMustWake = false;
pgPtr->pgSaverOn = false;
pgPtr->pgLastAction = LMGetTicks() + 60; /* can't sleep until a second from now */
/* Force all monitors to redraw, if required */
if (pgPtr->pgForceUpdates) {
/* Force all monitors to redraw */
theDevice = GetMainDevice();
do {
thePixMap = (*theDevice)->gdPMap;
theDepth = (*thePixMap)->pixelSize;
theFlags = (*theDevice)->gdFlags;
theError = SetDepth(theDevice, theDepth, 1, theFlags);
} while ((theDevice = (GDHandle) (*theDevice)->gdNextGD) != NULL);
DrawMenuBar();
}
}
/*********************************************************************
* DoAEOpenApp:
*
* Handle "Open Application" AppleEvents.
*
*********************************************************************/
pascal OSErr DoAEOpenApp(const AppleEvent *theAppleEvent, AppleEvent *replyAppleEvent, long refCon)
{
#pragma unused (theAppleEvent,replyAppleEvent,refCon)
return noErr;
}
/*********************************************************************
* DoAEQuitApp:
*
* Handle "Quit" AppleEvents.
*
*********************************************************************/
pascal OSErr DoAEQuitApp(const AppleEvent *theAppleEvent, AppleEvent *replyAppleEvent, long refCon)
{
#pragma unused (theAppleEvent,replyAppleEvent,refCon)
gRunning = false;
if (pgPtr->pgSaverOn == true)
WakeUp();
return noErr;
}
/*********************************************************************
* DoAEOpenDoc:
*
* Handle "Open Document" AppleEvents.
*
*********************************************************************/
pascal OSErr DoAEOpenDoc(const AppleEvent *theAppleEvent, AppleEvent *replyAppleEvent,
long refCon)
{
#pragma unused (theAppleEvent,replyAppleEvent,refCon)
return errAEEventNotHandled;
}
/*********************************************************************
* DoAEPrintDoc:
*
* Handle "Print Document" AppleEvents.
*
*********************************************************************/
pascal OSErr DoAEPrintDoc(const AppleEvent *theAppleEvent, AppleEvent *replyAppleEvent,
long refCon)
{
#pragma unused (theAppleEvent,replyAppleEvent,refCon)
return errAEEventNotHandled;
}